import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart';
import '../../services/file_service.dart';
import 'preview_utils.dart';

/// 文档预览组件
/// 用于显示文档类型文件的预览
class DocumentPreview extends StatelessWidget {
  final String? previewUrl;
  final String? fileName;
  final String? fileType;
  final String? extension;
  final String? mimeType;
  final int? fileSize;
  final bool directPreview;
  final VoidCallback? onRetry;

  const DocumentPreview({
    Key? key,
    required this.previewUrl,
    required this.fileName,
    this.fileType = 'document',
    this.extension,
    this.mimeType,
    this.fileSize,
    this.directPreview = false,
    this.onRetry,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          // 文件图标卡片
          Card(
            elevation: 4,
            shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.circular(16),
            ),
            color: Colors.white,
            child: Container(
              padding: const EdgeInsets.all(30),
              width: 180,
              height: 180,
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  // 文件图标
                  Icon(
                    PreviewUtils.getFileIcon(fileType, extension),
                    size: 80,
                    color: PreviewUtils.getFileColor(fileType),
                  ),
                  const SizedBox(height: 16),
                  // 文件扩展名
                  Container(
                    padding: const EdgeInsets.symmetric(
                      horizontal: 12,
                      vertical: 6,
                    ),
                    decoration: BoxDecoration(
                      color: PreviewUtils.getFileColor(
                        fileType,
                      ).withOpacity(0.1),
                      borderRadius: BorderRadius.circular(12),
                    ),
                    child: Text(
                      extension?.toUpperCase() ?? '未知',
                      style: TextStyle(
                        color: PreviewUtils.getFileColor(fileType),
                        fontWeight: FontWeight.bold,
                      ),
                    ),
                  ),
                ],
              ),
            ),
          ),
          const SizedBox(height: 24),

          // 文件名
          Padding(
            padding: const EdgeInsets.symmetric(horizontal: 32),
            child: Text(
              fileName ?? '未知文件',
              style: const TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
              textAlign: TextAlign.center,
            ),
          ),
          const SizedBox(height: 8),

          // 文件类型和大小
          Text(
            '${mimeType ?? "文档"} · ${fileSize != null ? FileService.formatFileSize(fileSize!) : "未知大小"}',
            style: const TextStyle(color: Colors.grey),
          ),
          const SizedBox(height: 36),

          // 下载/预览按钮
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              ElevatedButton.icon(
                onPressed: () async {
                  if (previewUrl != null) {
                    final url = Uri.parse(previewUrl!);
                    if (await canLaunchUrl(url)) {
                      await launchUrl(
                        url,
                        mode: LaunchMode.externalApplication,
                      );
                    }
                  }
                },
                icon: const Icon(Icons.download),
                label: const Text('下载文件'),
                style: ElevatedButton.styleFrom(
                  backgroundColor: Theme.of(context).primaryColor,
                  foregroundColor: Colors.white,
                  padding: const EdgeInsets.symmetric(
                    horizontal: 20,
                    vertical: 12,
                  ),
                ),
              ),
              const SizedBox(width: 16),
              // 如果支持在线预览，显示在线预览按钮
              if (directPreview)
                OutlinedButton.icon(
                  onPressed: () async {
                    if (previewUrl != null) {
                      final url = Uri.parse(previewUrl!);
                      if (await canLaunchUrl(url)) {
                        await launchUrl(url, mode: LaunchMode.inAppWebView);
                      }
                    }
                  },
                  icon: const Icon(Icons.visibility),
                  label: const Text('在线预览'),
                  style: OutlinedButton.styleFrom(
                    padding: const EdgeInsets.symmetric(
                      horizontal: 20,
                      vertical: 12,
                    ),
                  ),
                ),
            ],
          ),
        ],
      ),
    );
  }
}
